#ifndef PROEMULATOR_H
#define PROEMULATOR_H
#include <QWidget>
#include <button.h>
#include <QMessageBox>
#include <processes.h>
#include <QTime>
#include <QTableView>
#include <QStandardItemModel>
#include <QLabel>
#include <QPainter>
#include <thread.h>


class ProEmulator : public QWidget
{
    Q_OBJECT

    public slots:

        //-----------------------------------------------------------
        void add(){

            c++;
            Proces *d=new Proces(getRandom(7,1),getRandom(9,1),c,getRandom(2,0));
            proc->add(d);
            createTable();
            //tableProcess->
            //QMessageBox::information( this, "Information", ""+QString("%1").arg(proc->getMaxpriority()->priority));
        }

        //-----------------------------------------------------------
        void deleteP(){
            proc->remove(tableProcess->currentIndex().row());
            model->removeRow(tableProcess->currentIndex().row());
            //tableProcess->
            //QMessageBox::information( this, "Information", ""+QString("%1").arg(proc->getMaxpriority()->priority));
        }

        //-----------------------------------------------------------
        void playEmulator(){

            if(play->text()=="Play"){
                if(proc->size()==0){
                    QMessageBox::information( this, "Informacion", "debes agregar un proceso primero");
                }else{
                    play->setText("Pause");
                    newProces->setEnabled(false);
                    deleteProcess->setEnabled(false);
                    action->resume();
                    action->start();
                }
            }else{
                play->setText("Play");
                newProces->setEnabled(true);
                deleteProcess->setEnabled(true);
                action->stop();
                action->i=-2;
            }
        }

        void rePPaint(int i){
            y=i;
            repaint();
            createTable();
        }

    public:

        int y;
        Button *newProces;
        Button *deleteProcess;
        Button *play;
        Processes *proc;
        QTableView *tableProcess;
        int c;
        QStandardItemModel *model;
        QLabel *tTableP;
        QPainter *qp;
        Thread *action;

        //---------------------------------------
        //constructor
        ProEmulator(){
            y=0;
            resize(700,700);
            setWindowTitle("Emulador de procesos");
            launchWidget();
            launchEvents();
        }

        //---------------------------------------
        //destructor
        ~ProEmulator(){
            delete(action);
            delete(proc);
        }

    private:

        //----------------------------------------------------------
        void launchWidget(){

            c=0;

            proc=new Processes();

            newProces = new Button("Agregar Proceso",this);
            newProces->setGeometry(120,650,200,40);

            deleteProcess = new Button("Eliminar Proceso",this);
            deleteProcess->setGeometry(370,650,200,40);

            play = new Button("Play",this);

            action = new Thread();

            tableProcess=new QTableView(this);
            tableProcess->setGeometry(50,350,600,280);

            model = new QStandardItemModel(0,4,this);

            createTable();

            tableProcess->setModel(model);

            tTableP =new QLabel("Lista de Procesos",this);
            tTableP->setGeometry(50,325,120,25);

        }

        //-----------------------------------------------------------
        void launchEvents(){
            QObject::connect(newProces, SIGNAL(leftClick()),this ,SLOT(add()));
            QObject::connect(deleteProcess, SIGNAL(leftClick()),this ,SLOT(deleteP()));
            QObject::connect(play, SIGNAL(leftClick()),this ,SLOT(playEmulator()));
            QObject::connect(action, SIGNAL(repaint(int)),this ,SLOT(rePPaint(int)));
        }

        //----------------------------------------------------------------------------
        int getRandom(int h,int l){

            QTime time = QTime::currentTime();
            qsrand((uint)time.msec());

            return (qrand() % ((h + l) -l) +l);
        }

        //----------------------------------------------------------------------------
        void createTable(){

            Proces *aux=proc->head;
            model->clear();

            model->setHorizontalHeaderItem(0, new QStandardItem(QString("Nombre Proceso")));
            model->setHorizontalHeaderItem(1, new QStandardItem(QString("Prioridad")));
            model->setHorizontalHeaderItem(2, new QStandardItem(QString("Quatum")));
            model->setHorizontalHeaderItem(3, new QStandardItem(QString("PID")));
            model->setHorizontalHeaderItem(4, new QStandardItem(QString("Interactividad")));
            model->setHorizontalHeaderItem(5, new QStandardItem(QString("Estado")));

            while(aux){

                QList<QStandardItem*> newRow;

                QStandardItem* name = new QStandardItem(aux->name);
                name->setEditable(false);
                QStandardItem* pri = new QStandardItem(QString("%1").arg(aux->priority));
                pri->setEditable(false);
                QStandardItem* qua = new QStandardItem(QString("%1").arg(aux->quantum));
                qua->setEditable(false);
                QStandardItem* pid = new QStandardItem(QString("%1").arg(aux->pid));
                pid->setEditable(false);
                QStandardItem* inter = new QStandardItem(aux->inter==1?QString("Si"):QString("No"));
                inter->setEditable(false);
                QStandardItem* sta = new QStandardItem(QString("%1").arg(aux->state));
                sta->setEditable(false);

                newRow.append(name);
                newRow.append(pri);
                newRow.append(qua);
                newRow.append(pid);
                newRow.append(inter);
                newRow.append(sta);

                model->appendRow(newRow);

                aux=aux->next;
            }
        }


        //----------------------------------------------------------------------------
        void paintEvent(QPaintEvent *e){
            Q_UNUSED(e);

            qp=new QPainter(this);

            if(action->isRunning()){
                proc->setPainter(qp);
                proc->paintProcesses(y);

            }

            QPen pen(Qt::black, 2, Qt::SolidLine);
            qp->setPen(pen);
            qp->drawLine(0, 325, this->width(), 325);

            qp->drawText(320,10,QString("Ejecucion"));
            qp->setBrush(QBrush(Qt::green, Qt::SolidPattern));
            qp->drawEllipse(300,15,90,90);

            qp->drawText(520,95,QString("Suspendido"));
            qp->setBrush(QBrush(Qt::green, Qt::SolidPattern));
            qp->drawEllipse(500,100,90,90);

            qp->drawText(120,95,QString("Inactivo"));
            qp->setBrush(QBrush(Qt::green, Qt::SolidPattern));
            qp->drawEllipse(100,100,90,90);

            qp->drawText(320,195,QString("Preparado"));
            qp->setBrush(QBrush(Qt::green, Qt::SolidPattern));
            qp->drawEllipse(300,200,90,90);

            qp->end();
        }

};

#endif // PROEMULATOR_H
